﻿using System;
using System.Collections.Generic;
using System.Linq;
using Simple.Data;
using Model.User;

namespace Model.Course
{
    public class Course
    {
        protected String name;
        protected String description;
        protected Int32 id;
        protected Int32 lecturer;

        protected Course()
        {
        }

        protected Course(int id, String name, String description, int lecturer)
        {
            this.id = id;
            this.name = name;
            this.description = description;
            this.lecturer = lecturer;
        }

        public static Course get(Int32 id)
        {
            var db = Database.Open();
            var c = db.Course.Get(id);
            if (c != null)
                return new Course(c.id, c.name, c.description, c.lecturer);

            return null;
        }

        public static Course create(String name, String description, Teacher lecturer)
        {
            if (lecturer == null) return null;

            var db = Database.Open();
            var c = db.Course.Insert(Name: name, Description: description, Lecturer: lecturer.getId());

            if (c != null)
                return new Course(c.id, c.name, c.description, c.lecturer);

            return null;
        }

        // this is propably incorrect function and must be rewritten
        public static Course[] get(Teacher teacher)
        {
            var coursesRows = Database.Open().Lecture.FindAllByLecturer(teacher.getId());
            int count = Database.Open().Course.GetCount();

            Course[] courses = new Course[count];

            int i = 0;
            foreach (var t in coursesRows)
                courses[i++] = new Course(t.id, t.name, t.description, t.lecturer);

            return courses;
        }

        public static Course[] getAll()        
        {
            var coursesRows = Database.Open().Course.All();
            int count = Database.Open().Course.GetCount();

            Course[] courses = new Course[count];

            int i = 0;
            foreach (var t in coursesRows)
                courses[i++] = new Course(t.id, t.name, t.description, t.lecturer); 

            return courses;
        }

        public Student[] getSignedStudents()
        {
            var db = Database.Open();

            int c = db.SignedToCourse.GetCount(db.SignedToCourse.courseId == this.getId());
            var s = db.SignedToCourse.FindAllByCourseId(this.getId());

            Student[] student = new Student[c];
            int i = 0;

            foreach (var stu in s)
            {
                student[i++] = Student.get(stu.studentId);
            }

            return student;
        }

        public int getId()
        {
            return id;
        }

        public int getLecturerId()
        {
            return lecturer;
        }

        public Teacher getLecturer()
        {
            return new Teacher(Person.get(lecturer));
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
            Database.Open().Course.UpdateById(Id: id, Name: name);
        }

        public String getDescription()
        {
            return description;
        }

        public void setDescription(String description)
        {
            this.description = description;
            Database.Open().Course.UpdateById(Id: id, description: description);
        }
    }
}